﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System;
using System.Collections.Generic;
using Lcsoft.RssEngine.RssItem;
using Lcsoft.RssEngine.Shared;

namespace Lcsoft.RssEngine.Collections
{
	/// <summary>A strongly typed collection of <see cref="RssItem"/> objects</summary>
	public class RssItemCollection : List<RssItem.RssItem>
	{
		private DateTime _latestPubDate = RssDefault.DateTimeValue;
		private DateTime _oldestPubDate = RssDefault.DateTimeValue;
		private bool _pubDateChanged = true;

		/// <summary>Gets or sets the item at a specified index.<para>In C#, this property is the indexer for the class.</para></summary>
		/// <param name="index">The index of the collection to access.</param>
		/// <value>An item at each valid index.</value>
		/// <remarks>This method is an indexer that can be used to access the collection.</remarks>
		/// <exception cref="ArgumentOutOfRangeException">index is not a valid index.</exception>
		public new RssItem.RssItem this[int index]
		{
			get { return base[index]; }
			set
			{
				this._pubDateChanged = true;
				base[index] = value;
			}
		}

		/// <summary>Adds a specified item to this collection.</summary>
		/// <param name="item">The item to add.</param>
		/// <returns>The zero-based index of the added item.</returns>
		public new void Add(RssItem.RssItem item)
		{
			this._pubDateChanged = true;
			base.Add(item);
		}

		/// <summary>Inserts an item into this collection at a specified index.</summary>
		/// <param name="index">The zero-based index of the collection at which to insert the item.</param>
		/// <param name="item">The item to insert into this collection.</param>
		public new void Insert(int index, RssItem.RssItem item)
		{
			this._pubDateChanged = true;
			base.Insert(index, item);
		}

		/// <summary>Removes a specified item from this collection.</summary>
		/// <param name="item">The item to remove.</param>
		public new void Remove(RssItem.RssItem item)
		{
			this._pubDateChanged = true;
			base.Remove(item);
		}

		/// <summary>The latest pubDate in the items collection</summary>
		/// <value>The latest pubDate -or- RssDefault.DateTime if all item pubDates are not defined</value>
		public DateTime LatestPubDate()
		{
			this._CalculatePubDates();
			return this._latestPubDate;
		}

		/// <summary>The oldest pubDate in the items collection</summary>
		/// <value>The oldest pubDate -or- RssDefault.DateTime if all item pubDates are not defined</value>
		public DateTime OldestPubDate()
		{
			this._CalculatePubDates();
			return this._oldestPubDate;
		}

		/// <summary>Calculates the oldest and latest pubdates</summary>
		private void _CalculatePubDates()
		{
			if (!this._pubDateChanged) return;

			this._pubDateChanged = false;
			this._latestPubDate = DateTime.MinValue;
			this._oldestPubDate = DateTime.MaxValue;

			foreach (var item in this) if ((item.PubDate != RssDefault.DateTimeValue) & (item.PubDate > this._latestPubDate)) this._latestPubDate = item.PubDate;

			if (this._latestPubDate == DateTime.MinValue) this._latestPubDate = RssDefault.DateTimeValue;

			foreach (var item in this) if ((item.PubDate != RssDefault.DateTimeValue) & (item.PubDate < this._oldestPubDate)) this._oldestPubDate = item.PubDate;

			if (this._oldestPubDate == DateTime.MaxValue) this._oldestPubDate = RssDefault.DateTimeValue;
		}
	}
}
